<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>

</body>
<script>
  // ------------------------------------------------------------------------------------------------
  // 字符串操作: 单词分割，首字母大写,倒序
  let str = 'hello world baby'

  function upFirstLetterReserve(str) {
    const result = str.split(' ')
      .map(item => item.split('')[0].toUpperCase() + item.slice(1))
      .join(' ').split('')
      .reverse().join(' ')
    return result
  }
  const formatstr = upFirstLetterReserve(str)
  console.log(formatstr)

  // ------------------------------------------------------------------------------------------------
  //  回转13位加密

  //  https://zh.wikipedia.org/wiki/ROT13
  //  String.fromCharCode(str)     unicode => string
  //  str.charCodeAt(index)        string => unicode
  //  a-m 97-109
  //  n-z 110-122
  //  A-M 65-77
  //  N-Z 71-90

  function Rot13(message) {
    const result = message.split('').map((item, index) => {
      let unicodeValue = item.charCodeAt(0)
      if (unicodeValue >= 97 && unicodeValue <= 109) {
        unicodeValue += 13
      } else if (unicodeValue >= 110 && unicodeValue <= 122) {
        unicodeValue -= 13
      }
      if (unicodeValue >= 65 && unicodeValue <= 77) {
        unicodeValue += 13
      } else if (unicodeValue >= 71 && unicodeValue <= 90) {
        unicodeValue -= 13
      }
      return String.fromCharCode(unicodeValue)
    }).join('')
    console.log(result)
    return result
  }

  const message1 = 'test'
  const message2 = 'Test'
  Rot13(message1)
  Rot13(message2)
  // ------------------------------------------------------------------------------------------------

</script>

</html>
